package com.example.xiaomi.Model.Dao;

import com.example.xiaomi.Model.entity.UeserEntity;
import com.example.xiaomi.Model.entity.adddetailsEntity;
import com.example.xiaomi.Model.utils.BaseDB;
import com.example.xiaomi.Model.utils.RowMapper;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class adddetailsDao extends BaseDB {
    public List<adddetailsEntity> selectById(Integer did){
        String sql ="select * from is_details where did=?";
        return super.query(sql, new adddetailsDao.detailRowMapper(), did);
    }
    public Integer doUpdate(int did,int cid,String dmail,int damount,BigDecimal totalcost,int id){
        String sql="update is_details set did=?,cid=?,dmail=?,damount=?,totalcost=? where did="+id;
        return super.update(sql,did,cid,dmail,damount,totalcost);
    }
    public adddetailsEntity selectByMail(String Mail){
        String sql ="select * from is_details where dmail=?";
        List<adddetailsEntity> list = super.query(sql, new adddetailsDao.detailRowMapper(), Mail);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    public Integer doInsert(int did, int cid,String dmail, int damount, BigDecimal totalcost){
        String sql="insert into is_details values(?,?,?,?,?)";
        return super.insert(sql,did,cid,dmail,damount,totalcost);
    }
    public Integer doquantity(int id){
        String sql="select count(did) from is_details where did <=>"+id;
        return super.insert(sql,id);
    }
    class detailRowMapper implements RowMapper<adddetailsEntity> {
        @Override
        public adddetailsEntity rowmap(ResultSet rs) throws SQLException {
            return new adddetailsEntity(rs.getInt("did"),
                                        rs.getInt("cid"),
                                        rs.getString("dmail"),
                                        rs.getInt("damount"),
                                        rs.getBigDecimal("totalcost"));
        }
    }
}
